package craft;

import java.util.List;

public class SimpleTokenReader implements TokenReader{
    List<Token> tokens = null;
    int pos = 0;

    public SimpleTokenReader(List<Token> tokens) {
        this.tokens = tokens;
    }

    /**
     * 返回并取出当前的token
     * @return
     */
    public Token read() {
        if (pos < tokens.size()) {
            return tokens.get(pos++);
        }
        return null;
    }

    /**
     * 仅返回当前的token
     * @return
     */
    public Token peek() {
        if (pos < tokens.size()) {
            return tokens.get(pos);
        }
        return null;
    }

    public void unread() {
        if (pos > 0) {
            pos--;
        }
    }

    public int getPosition() {
        return pos;
    }


    public void setPosition(int position) {
        if (position >= 0 && position < tokens.size()) {
            this.pos = position;
        }
    }
}
